import {reqGoodInfo,reqAddOrUpdateShopCart} from "../../api";
import {getUUID} from "../../utils/uuid_token";

const state ={
    goodInfo:{},
    uuid_token:getUUID()
};
const mutations ={
    GETGOODINFO(state,goodInfo){
        state.goodInfo = goodInfo;
    }
};
const actions ={
    //获取产品信息的action
    async getGoodInfo({commit},skuid){
        let result = await reqGoodInfo(skuid)
        if(result.code ==200){
            commit('GETGOODINFO',result.data)
        }
    },
    async addOrUpdateShopCart({commit},{skuId,skuNum}){
        let result = await reqAddOrUpdateShopCart(skuId,skuNum);
        if(result.code ==200){
            return "ok"
        }else {
            return Promise.reject(new Error('加入购物车失败'));
        }
    }
};
//简化数据而生
const getters ={
    categoryView(state){
        //当前计算出的categoryView属性值至少是一个空对象，假的报错不会有
        return state.goodInfo.categoryView || {}
    },
    skuInfo(state){
        return state.goodInfo.skuInfo || {};
    },
    //产品售卖属性的简化
    supSaleAttrList(state){
        // console.log(state.goodInfo.spuSaleAttrList);
        return state.goodInfo.spuSaleAttrList||[]
    }
};

export default {
    state,
    actions,
    mutations,
    getters
}